package com.cg.service.impl;

import com.cg.dao.SalePlanMapper;
import com.cg.dao.SalePlanOperateMapper;
import com.cg.dao.UserMapper;
import com.cg.dto.CreateSalePlanDto;
import com.cg.dto.SalePlanPageDto;
import com.cg.entity.Role;
import com.cg.entity.SalePlan;
import com.cg.entity.SalePlanOperate;
import com.cg.entity.User;
import com.cg.service.SalePlanService;
import com.cg.vo.QuerySalePlanVo;
import com.cg.vo.SalePlanVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * (SalePlan)表服务实现类
 *
 * @author chengong
 * @since 2023-12-24 10:27:46
 */
@Service("salePlanService")
public class SalePlanServiceImpl implements SalePlanService {
    @Resource
    private SalePlanMapper salePlanMapper;

    @Resource
    private SalePlanOperateMapper salePlanOperateMapper;

    @Resource
    private UserMapper userMapper;


    /**
     * 销售计划管理列表
     */
    @Override
    public PageInfo<SalePlanVo> list(SalePlanPageDto salePlanPageDto) {
        PageHelper.startPage(salePlanPageDto.getPageNum(), salePlanPageDto.getPageSize());
        List<SalePlanVo> list = salePlanMapper.list(salePlanPageDto.getSalePlanMonth());
        PageInfo<SalePlanVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 创建销售计划
     */
    @Override
    @Transactional
    public Integer createSalePlan(CreateSalePlanDto createSalePlanDto) {
        SalePlan salePlan = createSalePlanDto.getSalePlan();
        //生成code
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        salePlan.setPlanCode(uuid);
        createSalePlanDto.getSalePlan().setPlanMonth(createSalePlanDto.getSalePlan().getPlanMonth() + "-01");
        //添加销售计划表信息
        Integer count = salePlanMapper.createSalePlan(salePlan);
        if (count <= 0) {
            return count;
        }
        //添加销售计划操作表信息
        //获取销售计划表id
        Integer salePlanId = createSalePlanDto.getSalePlan().getId();
        SalePlanOperate salePlanOperate = createSalePlanDto.getSalePlanOperate();
        salePlanOperate.setSalePlanId(salePlanId);
        count = salePlanOperateMapper.createSalePlanOperate(salePlanOperate);
        return count;
    }

    /**
     * 查询销售计划详细信息
     */
    @Override
    public QuerySalePlanVo querySalePlan(Integer salePlanId) {
        return salePlanMapper.querySalePlan(salePlanId);
    }

    /**
     * 加载所有的待处理人列表
     */
    @Override
    public List<User> pendingList(Integer userId) {
        //根据userId查询当前登录人的角色信息，一对多，一个用户可以对应多个角色信息
        List<Role> roleList = userMapper.queryRoleListByUserId(userId);
        if (roleList == null || roleList.size() == 0) {
            //该用户没有角色信息，或者登录失败
            return null;
        }
        String roleCode = null;
        for (Role role : roleList) {
            System.out.println(role.getRoleCode());
            if (role.getRoleCode().equals("SALE")) {
                //销售经理
                roleCode = "SALE";
                break;
            }
            if (role.getRoleCode().equals("SALE_1")) {
                //销售代表
                roleCode = "SALE_1";
            }
        }
        if (roleCode == null) {//既不是销售代表也不是销售经理
            return null;
        }
        if (roleCode.equals("SALE_1")) {//是销售代表
            //查询待处理人
            List<User> pendingList = salePlanMapper.pendingList("SALE");
            return pendingList;
        }
        //是销售经理
        List<User> pendingList = salePlanMapper.pendingList("SALE_3");
        return pendingList;
    }

    /**
     * 提交销售计划
     */
    @Override
    @Transactional
    public Integer submitSalePlan(CreateSalePlanDto createSalePlanDto) {
        Integer count = 0;
        //根据输入的销售计划id查询销售计划表里的对象
        //获取到销售计划id
        Integer salePlanId = createSalePlanDto.getSalePlan().getId();
        SalePlan salePlan = salePlanMapper.querySalePlanById(salePlanId);
        if (salePlan == null) {
            return count;
        }
        //判断当前创建人是否是提交的人
        if (!salePlan.getCreateUser().equals(createSalePlanDto.getSalePlan().getCreateUser())) {
            //当前创建人不是提交人
            return count;
        }
        //判断状态是否是创建状态或者打回状态
        Integer planState = salePlan.getPlanState();
        if (!(planState.equals(1) || planState.equals(3))) {
            return count;
        }
        //修改销售计划表的状态
        count = salePlanMapper.submitSalePlan(salePlanId);
        if (count == 0) {
            return count;
        }
        //添加销售计划表操作
        salePlanOperateMapper.submitSalePlanOperate(createSalePlanDto.getSalePlanOperate());
        return count;
    }

    /**
     * 打回销售计划，通过，作废操作
     */
    @Override
    @Transactional
    public Integer passSalePlan(CreateSalePlanDto createSalePlanDto) {
        Integer count = 0;
        //1、查询销售计划信息
        Integer salePlanId = createSalePlanDto.getSalePlan().getId();
        SalePlan salePlan = salePlanMapper.querySalePlanById(salePlanId);
        //2、判断当前状态是否已经是已提交的状态了，已提交的状态是2，可以做通过作废打回操作
        if (!salePlan.getPlanState().equals(2)) {
            //状态不是2，当前不是已提交的状态
            return count;
        }
        //3、判断当前处理人是不是当前登录人
        //获取销售计划操作表对象
        SalePlanOperate salePlanOperate = salePlanOperateMapper.querySalePlanOperateBySalePlanId(salePlanId);
        if (!salePlanOperate.getPendingUser().equals(createSalePlanDto.getSalePlan().getCreateUser())) {
            //处理人不是当前登录人
            return count;
        }
        //4、修改销售计划表
        Integer operateType = createSalePlanDto.getSalePlanOperate().getOperateType();
        if (operateType.equals(3)) {
            //打回操作
            count = salePlanMapper.repulshSalePlan(salePlanId);
            if (count == 0) {
                return count;
            }
        }
        if (operateType.equals(4)) {
            //通过操作
            count = salePlanMapper.passSalePlan(salePlanId);
            if(count == 0){
                return count;
            }
        }
        if (operateType.equals(5)) {
            //作废操作
            count = salePlanMapper.obsoleteSalePlan(salePlanId);
            if(count == 0){
                return count;
            }
        }
        //5、添加销售计划操作表记录
        salePlanOperateMapper.submitSalePlanOperate(createSalePlanDto.getSalePlanOperate());
        return count;
    }

    /**
     * 删除销售计划，修改销售计划
     */
    @Override
    @Transactional
    public Integer removeSalePlan(CreateSalePlanDto createSalePlanDto) {
        Integer count = 0;
        //根据id获取对象信息
        SalePlan salePlan = salePlanMapper.querySalePlanById(createSalePlanDto.getSalePlan().getId());
        if(salePlan == null){
            return count;
        }
        //判断状态
        if(!(salePlan.getPlanState().equals(1) || salePlan.getPlanState().equals(3))){
            return count;
        }
        //判断操作人是否和当前登录人相同
        Integer createUser = salePlan.getCreateUser();
        if(!createUser.equals(createSalePlanDto.getSalePlan().getCreateUser())){
            return count;
        }
        Integer operateType = createSalePlanDto.getSalePlanOperate().getOperateType();
        if(operateType.equals(8)){
            //删除
            count = salePlanMapper.removeSalePlan(createSalePlanDto.getSalePlan().getId());
            if(count == 0){
                return count;
            }
        }

        if(operateType.equals(7)){
            //删除
            count = salePlanMapper.updateSalePlan(createSalePlanDto.getSalePlan());
            if(count == 0){
                return count;
            }
        }

        //在销售计划操作表新添加记录
        salePlanOperateMapper.submitSalePlanOperate(createSalePlanDto.getSalePlanOperate());

        return count;
    }

}
